#include "delge_squirrelscreen.h"

MyScreen::MyScreen()
{
	this->sSquirrelName = "";
}

MyScreen::~MyScreen()
{
	this->sSquirrelName = "";
}

void MyScreen::Render()
{
	if (this->sSquirrelName != "")
	{
		SquirrelObject root = SquirrelVM::GetRootTable(); 
		SquirrelObject screen;
		SquirrelObject s_Render;

		try
		{
			screen = root.GetValue(this->sSquirrelName.c_str());
			s_Render = screen.GetValue("Render");
			SquirrelVM::BeginCall(s_Render);
			SquirrelVM::EndCall();
			root.Reset();
			screen.Reset();
			s_Render.Reset();
		}
		catch (SquirrelError& error)
		{
			QuitGame();
			return;
		}
	}
}

void MyScreen::Update(float fDelta)
{
	if (this->sSquirrelName != "")
	{
		SquirrelObject root = SquirrelVM::GetRootTable(); 
		SquirrelObject screen;
		SquirrelObject s_Update;

		try
		{
			screen = root.GetValue(this->sSquirrelName.c_str());
			s_Update = screen.GetValue("Update");	
			SquirrelVM::BeginCall(s_Update);
			SquirrelVM::PushParam(fDelta);
			SquirrelVM::EndCall();
			root.Reset();
			screen.Reset();
			s_Update.Reset();
		}
		catch (SquirrelError& error)
		{
			QuitGame();
			return;
		}
	}
}

void MyScreen::Load()
{
	if (this->sSquirrelName != "")
	{
		SquirrelObject root = SquirrelVM::GetRootTable(); 
		SquirrelObject screen;
		SquirrelObject s_Load;

		try
		{
			screen = root.GetValue(this->sSquirrelName.c_str());
			s_Load = screen.GetValue("Load");	
			SquirrelVM::BeginCall(s_Load);
			SquirrelVM::EndCall();
			root.Reset();
			screen.Reset();
			s_Load.Reset();
		}
		catch (SquirrelError& error)
		{
			QuitGame();
			return;
		}
	}
}

void MyScreen::Unload()
{
	if (this->sSquirrelName != "")
	{
		SquirrelObject root = SquirrelVM::GetRootTable(); 
		SquirrelObject screen;
		SquirrelObject s_Unload;

		try
		{
			screen = root.GetValue(this->sSquirrelName.c_str());
			s_Unload = screen.GetValue("Unload");	
			SquirrelVM::BeginCall(s_Unload);
			SquirrelVM::EndCall();
			root.Reset();
			screen.Reset();
			s_Unload.Reset();
		}
		catch (SquirrelError& error)
		{
			QuitGame();
			return;
		}
	}
}